package com.woniuxy.microcouponserver.couponInfomag.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.microcommonserver.bean.coupon_pzc.CouponInfo;
import com.woniuxy.microcommonserver.pageHelper.PageRequest;
import com.woniuxy.microcouponserver.couponInfomag.mapper.CouponInfoMapper;
import com.woniuxy.microcouponserver.couponInfomag.service.ICouponInfoService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

@Service
@Transactional(readOnly = true)
public class CouponInfoServiceImpl implements ICouponInfoService {

    @Resource
    private CouponInfoMapper couponInfoMapper;

    /**
     * 新增优惠券信息
     *
     * @param couponInfo 优惠券参数
     * @return 新增优惠券在mysql中的id
     */
    @Transactional(isolation = Isolation.REPEATABLE_READ,rollbackFor = Exception.class)
    @Override
    public int saveCouponInfo(CouponInfo couponInfo) {
        return couponInfoMapper.saveCouponInfo(couponInfo);
    }

    /**
     * 修改优惠券信息
     *
     * @param couponInfo id是要修改的对象，其他属性是要修改的内容
     * @return 受影响行数
     */
    @Transactional(isolation = Isolation.REPEATABLE_READ,rollbackFor = Exception.class)
    @Override
    public int updateCouponInfo(CouponInfo couponInfo) {
        return couponInfoMapper.updateCouponInfo(couponInfo);
    }

    /**
     * 根据id删除优惠券
     *
     * @param id 要删除的优惠券的id
     * @return 影响行数
     */
    @Transactional(isolation = Isolation.REPEATABLE_READ,rollbackFor = Exception.class)
    @Override
    public int deletedCouponInfo(Integer id) {
        return couponInfoMapper.deleteCouponInfo(id);
    }

    /**
     * 分页查找优惠券
     *
     * @param couponInfo  查询参数封装成对象
     * @param request 分页参数
     * @return 查询到的优惠券列表
     */
    @Override
    public PageInfo<CouponInfo> findAllByParams(CouponInfo couponInfo, PageRequest request) {
        System.out.println("=======service:"+couponInfoMapper.findAllByParams(request,couponInfo));
        return PageHelper.startPage(request.getPageNum(),request.getLimit()).
                doSelectPageInfo(()->couponInfoMapper.findAllByParams(request,couponInfo));
    }

    /**
     * 根据id查询优惠券
     *
     * @param id 优惠券id
     * @return 优惠券信息
     */
    @Override
    public CouponInfo findCouponInfoById(Integer id) {
        return couponInfoMapper.findById(id);
    }
}
